#include "door_fsm.hpp"

#include <iostream>

void DoorClosed::entry() {
  std::cout << "Door is now closed." << std::endl;
}

void DoorClosed::react(tinyfsm::Event const& e) {
  std::cout << "DoorClosed reacting to event" << std::endl;
  if (typeid(e) == typeid(OpenDoor)) {
    transit<DoorOpened>();
  }
}

void DoorOpened::entry() {
  std::cout << "Door is now opened." << std::endl;
}

void DoorOpened::react(tinyfsm::Event const& e) {
  std::cout << "DoorOpened reacting to event" << std::endl;
  if (typeid(e) == typeid(CloseDoor)) {
    transit<DoorClosed>();
  }
}

// 实例化 tinyfsm
template class tinyfsm::Fsm<DoorClosed>;
template class tinyfsm::Fsm<DoorOpened>;

// 为 DoorClosed 状态定义初始状态
namespace {
struct InitialStateClosed : DoorClosed {};
}  // namespace

template <>
void tinyfsm::Fsm<DoorClosed>::set_initial_state() {
  current_state_ptr = &_state_instance<InitialStateClosed>::value;
}

// 为 DoorOpened 状态定义初始状态
namespace {
struct InitialStateOpened : DoorOpened {};
}  // namespace

template <>
void tinyfsm::Fsm<DoorOpened>::set_initial_state() {
  current_state_ptr = &_state_instance<InitialStateOpened>::value;
}
